<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends Admin_Controller {

	public function __construct() {
		parent::__construct();
		//$this->output->enable_profiler(TRUE);
		$this->_addTitle('Manage Category');
	}
	
	public function index() {
		$this->loadLayout();
		$this->renderLayout();
	}
	
	public function edit() {
		//$this->output->enable_profiler(TRUE);

		// Get ID and create model
		$id = App::segment('id');
		$obj = App::object('Entity_Category');
		
		// Initial checking
		if ($id) $obj->load($id);
		$this->_addTitle($obj->getId() ? $obj->getName() : 'New Category');
		
		// Repopulate data if was error when we do save
		$data = App::objectSingleton('Entity_Session_Admin')->getFormData(true);
		if (!empty($data)) $obj->addData($data);
		
		// Register model to use later in blocks
		App::registry('form_object', $obj);
		
		// Render edit form
		$this->loadLayout();
		$this->renderLayout();
	}

	public function add() {
		$this->edit();
	}

	public function save() {
		//$this->output->enable_profiler(TRUE);
		// check if data sent
		if ($data = App::post()) {
			$id = App::post('id');
			$obj = App::object('Entity_Category')->load($id);

			// check if data exists when update existing object
			if (!$obj->getId() && $id) {
				App::objectSingleton('Entity_Session_Admin')->addError('This category no longer exists.');
				App::redirect( App::url('*/*/') );
				return;
			}

			$obj->addData($data);
			
			try {
				// save the data
				$obj->save();
				// display success message
				App::objectSingleton('Entity_Session_Admin')->addSuccess('The category has been saved.');
				// clear previously saved data from session
				App::objectSingleton('Entity_Session_Admin')->setFormData(null);

				// check if 'Save and Continue'
				if (App::segment('back')) {
					echo App::url('*/*/edit/'), array('id' => $obj->getId());
					App::redirect( App::url('*/*/edit/', array('id' => $obj->getId())) );
					return;
				}
				// go to grid
				App::redirect( App::url('*/*/') );
				return;

			} catch (Exception $e) {
				// display error message
				App::objectSingleton('Entity_Session_Admin')->addError($e->getMessage());
				// save data in session
				App::objectSingleton('Entity_Session_Admin')->setFormData($data);
				// redirect to edit form
				App::redirect( App::url('*/*/edit/', array('id' => $obj->getId())) );
				return;
			}
		}
		App::redirect( App::url('*/*/') );
	}

	public function delete() {
		// check if we know what should be deleted
		if ($id = App::segment('id')) {
			$title = "";
			try {
				// init model and delete
				$obj = App::object('Entity_Category')->load($id);
				$title = $obj->getTitle();
				$obj->delete();
				// display success message
				App::objectSingleton('Entity_Session_Admin')->addSuccess('The category has been deleted.');
				// go to grid
				App::redirect( App::url('*/*/') );
				return;

			} catch (Exception $e) {
				// display error message
				App::objectSingleton('Entity_Session_Admin')->addError($e->getMessage());
				// go back to edit form
				App::redirect( App::url('*/*/edit/', array('id' => $obj->getId())) );
				return;
			}
		}
		// display error message
		App::objectSingleton('Entity_Session_Admin')->addError('Unable to find a message to delete');
		// go to grid
		App::redirect( App::url('*/*/') );
	}
	
	public function reindex() {
		App::objectSingleton('Entity_Category_Cache')->refresh();
		App::objectSingleton('Entity_Session_Admin')->addSuccess('Re-index complete.');
		App::redirect( App::url('*/*/') );
	}
}
